layui.use(['table', 'form', 'layer', 'jquery'], function () {
    // 操作对象
    var form = layui.form
        , table = layui.table
        , layer = layui.layer
        , $ = layui.jquery;
    var width = window.screen.width;
    var height = window.screen.height;

    //列表
    var tableIns = table.render({
        elem: '#statisticsList',
        url : '/statistics/listData',
        cellMinWidth : 95,
        page : false,
        height : "full-125",
        limits : [10,20,30],
        limit : 10,
        response: {
            statusName: 'status' //规定数据状态的字段名称，默认：code
            , statusCode: 200 //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'total' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        },
        id : "statisticsListTable",
        cols : [[
            {field: 'Id',title: 'ID',type: 'checkbox',align: "center"},
            {field: 'androidDownloadsNum', title: '安卓下载量', minWidth:100, width:'20%', align:"center"},
            {field: 'iOSDownloadsNum', title: 'IOS下载量', minWidth:100, width:'20%', align:"center"},
            {field: 'installNum', title: '装机量', minWidth:100, width:'20%', align:"center"},
            {field: 'operateTpl',title: '操作',toolbar:'#barOption',align:"center"}
        ]]
    });

    table.on('tool(statisticsList)',function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

        //触发相应事件
        if(layEvent === 'del'){ //编辑
            layer.confirm('确定删除数据？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    type: "post",
                    url: "/statistics/del",
                    dataType: "json",
                    data: {
                        id:data.id
                    },
                    success: function (result) {
                        if(result.code == 0){
                            tableIns.reload();
                        }
                        layer.msg(result.msg);
                    }
                })
            })
        }else{
            layer.open({
                title: '修改',
                type: 2,
                area: ["480px",(width<1920&&height<1080)?"300px":"450px"],
                content: "/statistics/edit?id="+data.id,
                success: function (layero, index) {
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            })
        }
    });


    //切换站点
    form.on('select(site)', function (data) {
        $.ajax({
            type: "get",
            url: "/statistics/setSiteId/" + data.value,
            dataType: "json",
            success: function (data) {
                console.log(data)
                //$('#viewSiteId').html("当前站点："+data);
                location.reload();
            }, error: function (xhr, status, error) {
                layer.msg("服务器异常")
            }
        });
        return false;
    });

    $("#btn-add").on("click",function () {
        layer.open({
            title: '添加',
            type: 2,
            area: ["480px",(width<1920&&height<1080)?"300px":"450px"],
            content: "/statistics/addData",
            success: function (layero, index) {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
    })

    $(".search_button").on("click",function(){
        table.reload("statisticsListTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                queryContent: $("#queryContent").val()
            }
        })
    })



    $("#btn-refresh").on("click",function () {
        window.location.reload();
    })

})